/* --------------------------------------------------------
 * Copyright (c) aeky , Inc.  All rights reserved.
 * --------------------------------------------------------
 */
package com.aeky.client.view.login;

import com.aeky.client.common.AppParams;
import com.aeky.client.view.base.AekyWidget;
import com.smartgwt.client.types.KeyNames;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.PasswordItem;
import com.smartgwt.client.widgets.form.fields.TextItem;
import com.smartgwt.client.widgets.form.fields.events.KeyDownEvent;
import com.smartgwt.client.widgets.form.fields.events.KeyDownHandler;
import com.smartgwt.client.widgets.layout.VLayout;

/**
 * 
 * @author aeky
 */
public class Login extends AekyWidget {

    /** 登录按钮 */
    private IButton loginButton;

    /** 清空按钮 */
    private IButton clearButton;

    /** 用户名 */
    private TextItem loginName;

    /** 密码 */
    private PasswordItem password;

    private VLayout vLayout;

    private DynamicForm form;

    private LoginClickHandler loginClickHandler;

    public Login() {
        this.instanceWidgets();
        this.initDynamicForm();
        this.initEvents();
        vLayout.addMember(form);
        vLayout.addMember(loginButton);
        vLayout.addMember(clearButton);
        this.addMember(vLayout);
    }

    /**
     * Instance widgets.
     */
    private void instanceWidgets() {
        loginButton = new IButton("登录");
        clearButton = new IButton("清空");
        loginName = new TextItem();
        loginName.setValue("admin");
        loginName.setTitle("用户名");
        password = new PasswordItem();
        password.setValue("123456");
        password.setTitle("密码");
        vLayout = new VLayout();
        form = new DynamicForm();
    }

    private void initDynamicForm() {
        form.setWidth("300");
        form.setFields(loginName, password);
    }

    private void initEvents() {
        loginButton.addClickHandler(this.getLoginClickHandler());
        password.addKeyDownHandler(new KeyDownHandler() {
            public void onKeyDown(KeyDownEvent event) {
                String keyName = event.getKeyName();
                if (KeyNames.ENTER.equals(keyName)) {
                    getLoginClickHandler().onClick(new ClickEvent(loginButton.getJsObj()));
                }
            }
        });
        clearButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                AppParams.getInstance().getApp().getContentPanel().clear();
                // Login.this.redraw();
                // Login.this.show();
            }
        });
    }

    private ClickHandler getLoginClickHandler() {
        if (this.loginClickHandler == null) {
            this.loginClickHandler = new LoginClickHandler() {
                String getLoginName() {
                    return loginName.getValue() + "";
                }

                String getPassword() {
                    return password.getValue() + "";
                }
            };
        }
        return loginClickHandler;
    }
}
